package com.fast.user.controller;

import com.fast.user.common.constants.Constants;
import com.fast.user.service.UserServcie;
import com.wmm.common.bean.ResponseResultDTO;
import com.wmm.common.bean.user.UserDTO;
import com.wmm.common.bean.user.UserElement;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

/**
 * @author  wangzun
 * @TODO	微博系统用户服务组件-个性化推荐服务
 * */
@RestController
@RequestMapping("/api/user")
public class UserController {
	
	private static Logger log = LoggerFactory.getLogger(UserController.class);
	
	@Autowired
	private UserServcie userService;

	@GetMapping("/find/{userId}")
	public ResponseResultDTO<UserDTO> find(@PathVariable String userId) {
		ResponseResultDTO<UserDTO> response = userService.find(userId);
		return response;
	}

	@RequestMapping("/auto/find/{userId}")
	public ResponseResultDTO<UserDTO> finduser(@PathVariable String userId) {
		ResponseResultDTO<UserDTO> response = new ResponseResultDTO<UserDTO>();
		response = userService.find(userId);
		return response;
	}



	/**
	 *
	 * @Description   用户登录
	 * @Author WangZun
	 * @Date 2018/06/17 11:10
	 * @Param [session, user]
	 * @Return com.wmm.common.bean.ResponseResultDTO
	 */
	@PostMapping("/login")
	public ResponseResultDTO<UserElement> login(@RequestBody UserDTO user, HttpSession session) {
		ResponseResultDTO<UserElement> response = new ResponseResultDTO<UserElement>();
		UserElement ue = userService.login(user);
		if(ue != null) {
			if(session.getAttribute(Constants.REQUEST_USER_SESSION) == null) {
				session.setAttribute(Constants.REQUEST_USER_SESSION,ue);
			}
			response.setData(ue);
		}
		return response;
	}


}
